package cn.catkic.location;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.TelephonyManager;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

public class LocateService extends Service implements LocationListener {
    private static final int NOTICE_ID = 100;
    private String TAG = LocateService.class.getName();
    private LocationManager locationManager;
    private int point = 0;
    private Notification.Builder builder;
    private NotificationManager notificationManager;

    public LocateService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentTitle("测试");
        builder.setContentText("测试...");

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String chanel_id = "sms_chanel";
            String name = "监听短信";
            NotificationChannel mChannel = new NotificationChannel(chanel_id, name, NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(mChannel);
            mChannel.enableLights(true); //是否在桌面icon右上角展示小红点
            mChannel.setLightColor(Color.GREEN); //小红点颜色
            builder.setChannelId(chanel_id);
        } else {

        }

        startForeground(NOTICE_ID, builder.build());

        locate();

        // 如果觉得常驻通知栏体验不好
        // 可以通过启动CancelNoticeService，将通知移除，oom_adj值不变
//            Intent intent = new Intent(this,CancelNoticeService.class);
//            startService(intent);
//        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
//        IntentFilter intentFilter = new IntentFilter();
//        intentFilter.addAction("android.provider.Telephony.SMS_RECEIVED");
//        registerReceiver(broadcastReceiver, intentFilter);

    }

    @SuppressLint("MissingPermission")
    private void locate() {
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//        locationManager.getBestProvider()
//        locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, this, Looper.myLooper());
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 0, this);

//        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//        if (location != null) {
//            Snackbar.make(view, location.getLongitude() + "Replace with your own action" + location.getLatitude(), Snackbar.LENGTH_LONG)
//                    .setAction("Action", null).show();
//        }

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 如果Service被终止
        // 当资源允许情况下，重启service
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // 如果Service被杀死，干掉通知
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            NotificationManager mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            mManager.cancel(NOTICE_ID);
        }
        // 重启自己
        Intent intent = new Intent(getApplicationContext(), LocateService.class);
        startService(intent);
    }

    @Override
    public void onLocationChanged(Location location) {
        point++;
        String contentText = location.getLongitude() + " " + location.getLatitude();
        builder.setContentText(point + " 定位点");

        builder.setSubText(contentText);
        notificationManager.notify(NOTICE_ID, builder.build());
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}
